package com.edu.hit.showdemo;

import cn.edu.hit.core.Document;
import cn.edu.hit.core.db.*;
import cn.edu.hit.kg.data.GraphData;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class getStaticData {
    static DataConnection connection = DataConnectionLoader.getInstance();
    static DataReader reader = connection.getReader();
    static DBReadHelper dbHelper = new DBReadHelper(reader);
    static GraphData graphData = GraphData.getInstance();

    public static List<Map<String, Object>> listFaults() {
        return graphData.getByLabel("Fault")
//                .filter(node -> node.asMap().containsKey("outId") && node.get("outId").toString().length()>=7)
                .map(node -> {
                    Map<String, Object> nodeMap = new HashMap<>();
                    nodeMap.put("identity", node.id());
                    nodeMap.put("labels", node.labels());
                    nodeMap.put("properties", node.asMap());
//                    nodeMap.put("ports", getFaultPort(node.id()));
                    return nodeMap;
                }).collect(Collectors.toList());
    }

    public static List<Map> getPort() {
        List<Document> channelList = reader.queryByTemplate(Filters.eq("type", "signal"));
        List<Map> list = new ArrayList<>();
        channelList.forEach(channel -> {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("portName",channel.getString("portName"));
            jsonObject.put("dataSourceArgs",channel.getString("dataSourceArgs"));
            jsonObject.put("displayForm",channel.getString("displayForm"));
            jsonObject.put("dataSource",channel.getString("dataSource"));
            jsonObject.put("MaxValue",channel.getString("MaxValue"));
            jsonObject.put("MinValue",channel.getString("MinValue"));
            list.add(jsonObject);
        });
        return list;
    }

    public static List<Map> getMachines() {
        List<Document> channelList = reader.queryByTemplate(Filters.eq("type", "generator"));
        List<Map> list = new ArrayList<>();
        channelList.forEach(channel -> {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("MachineName",channel.getString("MachineName"));
            jsonObject.put("MachineNo",channel.getString("MachineNo"));
            list.add(jsonObject);
        });
        return list;
    }

}
